<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/shop_logo.png" alt="logo">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#474d5e" text-color="#fff" active-text-color="#ffd04b" unique-opened
          :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click="saveNavState('/' + subItem.path)">
              <i class="el-icon-menu"></i>
              <!--文本-->
              <span>{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
 
<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],  // 左侧导航菜单数据
      iconsObj: {  // 字体图标数据
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      isCollapse: false,  // 侧边栏折叠标识,
      activePath:'',  // 被激活的链接地址
    }
  },
  created() {
    this.getMenuList();  // 获取所有菜单
    this.activePath = window.sessionStorage.getItem('activePath')   // 激活的链接地址
  },
  methods: {
    loginout() {
      window.sessionStorage.clear();
      this.$router.push('/login')
      this.$message.success('退出成功！')
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 面板收缩
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
}
</script>
 
<style lang='less' scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #454c51;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  align-items: center;
  color: white;
  font-size: 25px;
  font-family: '楷体';

  >div {
    display: flex;
    align-items: center;

    img {
      width: 60px;
      height: 60px;
    }

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #474d5e;

  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 6px;
}

.toggle-button {
  background-color: #737a8e;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.3em; // 文字间距
  cursor: pointer; // 小手鼠标
}
</style>